﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using V5_EaspLibs;
using System.Data;
using System.Xml;
using V5_EaspLibs.Core;
using V5_EaspLibs.FrameWork.Model;

namespace V5_FrameWork.Data {
    /// <summary>
    /// 网站全局配置帮助
    /// </summary>
    public class BaseConfigHelper {
        private string xmlConfingPath = "Config/V5Config.config";
        public XmlSetHelper xmlSet = null;
        public BaseConfigHelper() {
           // xmlSet = new XmlSetHelper(xmlConfingPath, "configs");
           // xmlSet.LoadDom();
        }
        #region 访问器
        private string _TemplatePath = string.Empty;
        /// <summary>
        /// 模板路径
        /// </summary>
        public string TemplatePath {
            get {
                _TemplatePath = "/Template/" + xmlSet.Items["templatedir"] + "/";
                return _TemplatePath;
            }
            set { _TemplatePath = value; }
        }

        private string _TemplateDir = string.Empty;
        /// <summary>
        /// 模板目录
        /// </summary>
        public string TemplateDir {
            get {
                _TemplateDir = xmlSet.Items["templatedir"].ToString();
                return _TemplateDir;
            }
            set { _TemplateDir = value; }
        }
        #endregion

        #region 方法
        /// <summary>
        /// 获取配置文件中的节点
        /// </summary>
        /// <param name="setName">节点名称</param>
        /// <returns></returns>
        public string GetXmlConfigNode(string setName) {
            return xmlSet.Items[setName.ToLower()].ToString();
        }
        /// <summary>
        /// 保存配置文件中节点
        /// </summary>
        /// <param name="xmlPath">配置文件路径</param>
        /// <param name="xmlName">节点名称</param>
        /// <param name="xmlValue">节点值</param>
        public void SaveXmlConfigNode(string xmlPath, string xmlName, string xmlValue) {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(xmlPath);
            XmlNodeList xmlNodeList = xmlDoc.SelectNodes("configs/" + xmlName);
            XmlNode xmlNode = xmlNodeList.Item(0);
            xmlNode.InnerText = xmlValue;
            xmlDoc.Save(xmlPath);
        }
        /// <summary>
        /// 保存配置文件中节点
        /// </summary>
        /// <param name="xmlName">节点名称</param>
        /// <param name="xmlValue">节点值</param>
        public void SaveXmlConfigNode(string xmlName, string xmlValue) {
            xmlSet.Items[xmlName] = xmlValue;
            xmlSet.SaveDom();
        }
        /// <summary>
        /// 获取配置节点值
        /// </summary>
        /// <param name="nodeName"></param>
        /// <returns></returns>
        public string GetXmlConfigNodeValue(string nodeName) {
            try {
                if (string.IsNullOrEmpty(nodeName))
                    return "";
                return Easp.Str.SetString(xmlSet.Items[nodeName.ToLower()]);
            }
            catch (Exception) {
                return "";
            }

        }
        /// <summary>
        /// 重新载入节点
        /// </summary>
        public void ClearReSetXmlConfig() {
            xmlSet.LoadDom();
        }
        /// <summary>
        /// 获取所有皮肤
        /// </summary>
        /// <param name="xmlPath">皮肤路径</param>
        /// <returns></returns>
        public List<ModelSkin> GetAllSkin(string xmlPath) {
            XmlDocument xdoc = new XmlDocument();
            xdoc.Load(xmlPath);
            XmlElement root = xdoc.DocumentElement;
            XmlNodeList elemList = root.GetElementsByTagName("skinname");
            XmlNodeList elemList1 = root.GetElementsByTagName("skindir");
            List<ModelSkin> listSkinItem = new List<ModelSkin>();
            for (int i = 0; i < elemList.Count; i++) {
                ModelSkin si = new ModelSkin();
                si.SkinName = elemList[i].InnerXml.ToString();
                si.SkinDir = elemList1[i].InnerXml.ToString();
                listSkinItem.Add(si);
            }
            return listSkinItem;
        }
        #endregion
    }
}
